// pages/menu/menu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectOpen: ["自取", "配送"],
    _num: 0,
    menuHeight: 0,

    toView: 'a1',
    activeId: 'a1',
    category: [{
        name: '人气top',
        id: 'a1'
      },
      {
        name: 'Dirty系列',
        id: 'a2'
      },
      {
        name: '大师咖啡',
        id: 'a3'
      },
      {
        name: '小黑杯',
        id: 'a4'
      },
      {
        name: '生酪拿铁',
        id: 'a5'
      },
      {
        name: "生椰家族",
        id: 'a6'
      },
      {
        name: '丝绒拿铁',
        id: 'a7'
      },
      {
        name: '不喝咖啡',
        id: 'a8'
      },
    ],
    productList: [{
        id: "a1",
        categoryName: "人气top",
        categoryDeclare: "帅",
        items: [{
            id: 1,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:3,
          },
          {
            id: 2,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          }
        ]
      },
      {
        id: "a2",
        categoryName: "Dirty系列筹",
        categoryDeclare: "帅到爆",
        items: [{
            id: 3,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 440,
            inCartCount:0
          },
          {
            id: 4,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 5,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          }
        ]
      },
      {
        id: "a3",
        categoryName: "大师咖啡",
        categoryDeclare: "帅到爆",
        items: [{
            id: 6,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 440,
            inCartCount:0
          },
          {
            id: 7,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 8,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
        ]
      },
      {
        id: "a4",
        categoryName: "小黑杯",
        categoryDeclare: "帅到爆",
        items: [{
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
          {
            id: 9,
            image: "/assets/rcoffice.png",
            name: "圣夜拿铁",
            specialDec: "原创",
            contentDesc: "年度重磅666",
            price: 10,
            originPrice: 30,
            inCartCount:0
          },
        ]
      }
    ],
    showCart: true,
    cartCount: 1,

    // 购物车内容
    shopCardProduts: [{
      id: 9,
      image: "/assets/rcoffice.png",
      name: "圣夜拿铁",
      specialDec: "原创",
      contentDesc: "年度重磅666",
      price: 10,
      originPrice: 30
    }, {
      id: 9,
      image: "/assets/rcoffice.png",
      name: "圣夜拿铁",
      specialDec: "原创",
      contentDesc: "年度重磅666",
      price: 10,
      originPrice: 30
    }, {
      id: 9,
      image: "/assets/rcoffice.png",
      name: "圣夜拿铁",
      specialDec: "原创",
      contentDesc: "年度重磅666",
      price: 10,
      originPrice: 30
    }, {
      id: 9,
      image: "/assets/rcoffice.png",
      name: "圣夜拿铁",
      specialDec: "原创",
      contentDesc: "年度重磅666",
      price: 10,
      originPrice: 30
    }, {
      id: 9,
      image: "/assets/rcoffice.png",
      name: "圣夜拿铁",
      specialDec: "原创",
      contentDesc: "年度重磅666",
      price: 10,
      originPrice: 30
    }],
    // 是否显示购物车内容
    isShowShopCartProduct: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let screenHeight = wx.getSystemInfoSync().windowHeight;
    this.setData({
      menuHeight: screenHeight - 80,
    });


    this.setData({
      toView: 'a1',
      heightArr: []
    })
    let query = wx.createSelectorQuery();
    query.selectAll('.menu_right_area_item').boundingClientRect((rect) => {
      rect.forEach(ele => {
        this.calculateHeight(ele.height);
      })
    }).exec();
    if (this.data.cartCount > 0 && this.data.showCart) {
      this.showShopingCartSummary()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  switchChange(item) {
    console.log(item.currentTarget.dataset.index)
    this.setData({  
      _num: item.currentTarget.dataset.index
    })
    this.gotoShopListPage()
  },

  // 点击左边tab
  clickItem(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      activeId: e.currentTarget.dataset.id,
      toView: e.currentTarget.dataset.id
    })
  },
  scroll(e) {
    let scrollHeight = e.detail.scrollTop + 80;
    let index = this.calculateIndex(this.data.heightArr, scrollHeight);
    this.setData({
      activeId: 'a' + index
    })

  },
  // 计算滚动的区间
  calculateHeight(height) {
    console.log(height)
    if (!this.data.heightArr.length) {
      this.data.heightArr.push(height)
    } else {
      this.data.heightArr.forEach(ele => {
        height += ele
      })
      this.data.heightArr.push(height);
    }
  },
  // 计算左边选中的下标
  calculateIndex(arr, scrollHeight) {
    let index = '';
    for (let i = 0; i < arr.length; i++) {
      if (scrollHeight >= 0 && scrollHeight < arr[0]) {
        index = 0;
      } else if (scrollHeight >= arr[i - 1] && scrollHeight < arr[i]) {
        index = i;
      }
    }
    return index + 1;
  },
  //跳转下单
  gotoOrderPage() {
    wx.navigateTo({
      url: '/pages/productDetail/productDetail',
    })
  },

  // 点击隐藏弹出蒙版
  showShopingCartSummary() {
    if (!this.data.showCart) {
      return
    }
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation;
    // animation.opacity(0).step();
    animation.translateX(1000).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateX(0).step()
      // animation.opacity(1).step();
      this.setData({
        animationData: animation.export(),
        showCart: false
      })
    }.bind(this), 200)

  },
  // 显示购物车-产品页面
  showShopCartProeudt() {
    this.setData({
      isShowShopCartProduct: false
    })
  },
  // 显示购物车-产品页面
  hiddenShopCartProeudt() {
    this.setData({
      isShowShopCartProduct: true
    })
  },
  //跳转下单
  gotoConfirmOrder() {
    wx.navigateTo({
      url: '/pages/confirmOrder/confirmOrder',
    })
  },

  gotoShopListPage(){
    wx.navigateTo({
      url: '/pages/shopAddress/shopAddress',
    })
  }
})